/*
 * =========================================================================
 * Copyright 2009 NCS Pte. Ltd. All Rights Reserved This software is
 * confidential and proprietary to NCS Pte. Ltd. You shall use this software
 * only in accordance with the terms of the license agreement you entered into
 * with NCS. No aspect or part or all of this software may be reproduced,
 * modified or disclosed without full and direct written authorisation from NCS.
 * NCS SUPPLIES THIS SOFTWARE ON AN AS IS BASIS. NCS MAKES NO REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE SUITABILITY OR
 * NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE FOR ANY LOSSES OR
 * DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES. Change Revision
 * ------------------------------------------------------------------------- Apr
 * 15, 2009 yunfeng Description
 */
package canglang.scan.core.base.pagination;

import java.io.Serializable;
import java.util.List;

import canglang.scan.core.base.taglib.displaytag.pagination.PaginatedList;

@SuppressWarnings("unchecked")
public class PaginatedListHelper implements PaginatedList, Serializable {

    private static final long serialVersionUID = 1985760358956293042L;

    private int maxListSize = Integer.MAX_VALUE;
    private boolean reachMax = false;

    public static final String REC_PER_PAGE = "REC_PER_PAGE";
    public static final String CURRENT_RANGE = "CURRENT_RANGE";
    public static final String SORT_DIR = "SORT_DIR";
    public static final String SORT_FIELD = "SORT_FIELD";

    private int pageNumber = 1;
    private int oldPageNumber = 1;
    private int objectsPerPage = 10;
    private int fullListSize = 0;

    private String sortCriterion;
    private String oldSortCriterion;
    private String sortDirection;
    private String oldSortDirection;
    private String searchId;
    private List oList;
    private List exportList;
    private String exportType;
    private String sortTag;

    public String getSortTag () {
        return sortTag;
    }

    public void setSortTag (String sortTag) {
        this.sortTag = sortTag;
    }

    public String getExportType () {
        return exportType;
    }

    public void setExportType (String exportType) {
        this.exportType = exportType;
    }

    public List getExportList () {
        return exportList;
    }

    public void setExportList (List exportList) {
        this.exportList = exportList;
    }

    public int getPageNumber () {
        if (pageNumber > getTotalPage(fullListSize,objectsPerPage))
            pageNumber = getTotalPage(fullListSize,objectsPerPage);
        if (pageNumber == 0)
            pageNumber = 1;
        return pageNumber;
    }

    public void setPageNumber (int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getObjectsPerPage () {
        return objectsPerPage;
    }

    public void setObjectsPerPage (int objectsPerPage) {
        this.objectsPerPage = objectsPerPage;
    }

    public int getFullListSize () {
        return fullListSize;
    }

    public void setFullListSize (int fullListSize) {
        if (fullListSize > maxListSize) {
            this.fullListSize = maxListSize;
            reachMax = true;
        } else {
            reachMax = false;
            this.fullListSize = fullListSize;
        }
    }

    public String getSortCriterion () {
        return sortCriterion == null ? "1" : sortCriterion;
    }

    public void setSortCriterion (String sortCriterion) {
        this.sortCriterion = sortCriterion;
    }

    public void setSortDirection (String dir) {
        if (dir == null || "null".equals(dir) || "desc".equalsIgnoreCase(dir))
            sortDirection = "desc";
        else
            sortDirection = "asc";
    }

    public String getSortDirection () {
        return sortDirection == null ? "desc" : sortDirection;
    }

    public String getSearchId () {
        return searchId;
    }

    public void setSearchId (String searchId) {
        this.searchId = searchId;
    }

    public int getFromIndex () {
        while ((pageNumber - 1) * objectsPerPage + 1 > fullListSize && pageNumber > 1 && fullListSize != 0) {
            pageNumber--;
        }
        return (pageNumber - 1) * objectsPerPage + 1;
    }

    public int getToIndex () {
        return pageNumber * objectsPerPage;
    }

    public List<Object> getList () {
        return oList;
    }

    public void setList (List<Object> list) {
        oList = list;

    }

    public int getTotalPageNumber () {
        if (fullListSize % objectsPerPage == 0)
            return fullListSize / objectsPerPage;
        return fullListSize / objectsPerPage + 1;
    }

    public int getOldPageNumber () {
        return oldPageNumber;
    }

    public void setOldPageNumber (int oldPageNumber) {
        this.oldPageNumber = oldPageNumber;
    }

    public String getOldSortCriterion () {
        return oldSortCriterion;
    }

    public void setOldSortCriterion (String oldSortCriterion) {
        this.oldSortCriterion = oldSortCriterion;
    }

    public String getOldSortDirection () {
        return oldSortDirection;
    }

    public void setOldSortDirection (String oldSortDirection) {
        this.oldSortDirection = oldSortDirection;
    }

    /**
     * @param maxListSize the maxListSize to set
     */
    public void setMaxListSize (int maxListSize) {
        this.maxListSize = maxListSize;
    }

    /**
     * @return the reachMax
     */
    public boolean isReachMax () {
        return reachMax;
    }

    public PaginatedListHelper () {

    }

    public PaginatedListHelper (int objectsPerPage) {
        this.objectsPerPage = objectsPerPage;
    }

    private int getTotalPage(int a,int b){
        return (((double)a/(double)b)>(a/b)?a/b+1:a/b);
    }
 
}
